package com.artqiyi.dollmachine.views.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.utils.Rx2Timer;
import com.bumptech.glide.Glide;

import java.util.concurrent.TimeUnit;

import timber.log.Timber;


/**
 * Mainly used for confirmation and cancel.
 */
public class RxDialogSureCancel extends RxDialog {

    private Context mContext;
    private TextView mTvCancel;
    private TextView mTvSure;
    private ImageView mImageSuccess;
    private ImageView mImageFail;
    private TextView mTvCountDown;

    public RxDialogSureCancel(Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
        initView();
    }

    public RxDialogSureCancel(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mContext = context;
        initView();
    }

    public RxDialogSureCancel(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public RxDialogSureCancel(Activity context) {
        super(context);
        mContext = context;
        initView();
    }

    public RxDialogSureCancel(Context context, float alpha, int gravity) {
        super(context, alpha, gravity);
        mContext = context;
        initView();
    }

    public TextView getSureView() {
        return mTvSure;
    }

    public void setCancel(String strCancel) {
        this.mTvCancel.setText(strCancel);
    }

    public void setSure(String strSure) {
        this.mTvSure.setText(strSure);
    }

    public TextView getCancelView() {
        return mTvCancel;
    }

    public void setCountDown() {
        doCountDown();
    }

    public void cancelCountDown() {
        if (countDownTimer != null) {
            countDownTimer.stop();
            countDownTimer = null;
        }
    }

    public void setSureListener(View.OnClickListener sureListener) {
        mTvSure.setOnClickListener(sureListener);
    }

    public void setCancelListener(View.OnClickListener cancelListener) {
        mTvCancel.setOnClickListener(cancelListener);
    }

    public void setRxDialogResult(RxDialogResult result) {
        if (result == RxDialogResult.SUCCESS) {
            mImageSuccess.setVisibility(View.VISIBLE);
            mImageFail.setVisibility(View.GONE);
        } else {
            mImageSuccess.setVisibility(View.GONE);
            mImageFail.setVisibility(View.VISIBLE);
        }
    }

    private void initView() {
        View dialog_view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_sure_false, null);
        mTvSure = (TextView) dialog_view.findViewById(R.id.tv_sure);
        mTvCancel = (TextView) dialog_view.findViewById(R.id.tv_cancel);
        mImageSuccess = (ImageView) dialog_view.findViewById(R.id.imageSuccess);
        mImageFail = (ImageView) dialog_view.findViewById(R.id.imageFail);
        mTvCountDown = (TextView) dialog_view.findViewById(R.id.tv_countdown);
        setContentView(dialog_view);
        setCanceledOnTouchOutside(false);
    }

    public enum RxDialogResult {
        SUCCESS, FAIL
    }

    private Rx2Timer countDownTimer;    //倒计时

    private void doCountDown() {
        if (countDownTimer != null) {
            countDownTimer.stop();
        }
        countDownTimer = Rx2Timer.builder()
                .initialDelay(0) //default is 0
                .period(1) //default is 1
                .take(5) //default is 60
                .unit(TimeUnit.SECONDS) // default is TimeUnit.SECONDS
                .onEmit(count -> {
                    mTvCountDown.setText("(" + count + ")");
                })
                .onError(e -> {
                    mTvCountDown.setText("");
                })
                .onComplete(() -> {
                    if (mOnCountDownCompleteLister != null) {
                        mOnCountDownCompleteLister.onComplete();
                    }
                })
                .build();
        countDownTimer.start();
    }

    private OnCountDownCompleteLister mOnCountDownCompleteLister;
    public void setOnCountDownCompleteLister(OnCountDownCompleteLister onCountDownCompleteLister) {
        mOnCountDownCompleteLister = onCountDownCompleteLister;
    }
    public interface OnCountDownCompleteLister {
        void onComplete();
    }
}
